<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\common\logic\home;
use think\Db;
use app\common\model\TemplateAttrModel;

/**
 * 课题
 */
class Mytask
{
	/**
	 * [获取课题详情]
	 * @param  [type] $mytask_id [课题ID]
	 * @param  [type] $user_id   [用户ID]
	 * @return [type]            [description]
	 */
	public function index($mytask_id,$user_id){
        $data=Db::name('mytask')->where('id='.$mytask_id)->find();
        $content=json_decode($data['content'],true);

        $list=array();
        if($content){
            foreach($content as $key=>$val){
                $reply=$this->getanser($user_id,$mytask_id,$val['id']);
    
                if(!empty($reply)){
                    $val['reply'] = explode(',&lt;&gt;',$reply[0]['content']); 

                    $val['reply_evaluate'] = $reply[0]['evaluate']; 

                }else{
                    $val['reply_evaluate'] = null; 

                }
                $val['anser_param'] = ['mytask_id'=>$mytask_id,'user_id'=>$user_id,'classprogressid'=>$val['id']];
                $list['template'][$val['progressid']]['content'][]=$val;
                //$list[$val['progressid']]['name']=$val['name'];
            }
            //子标题
            foreach ($list['template'] as $key => $value) {
                $name = TemplateAttrModel::where('template_id',$value['content'][0]['mid'])->where('progress',$key)->value('name');
                $list['names'][$key] = empty($name) ? '' : $name;
            }
        }
        
        

        //标题
        $list['name'] = $data['name'];
        //类型 1 课题 2报告
        $list['type'] = $data['type'];
        //ID
        $list['id'] = $data['id'];
        //课题提交评审状态
        $list['add_status'] = $data['add_status'];

        //表头
        $list['header'] = '';
        if(!empty($data['header'])){
            $header = json_decode($data['header'],true);
            $list['header'] = json_decode($header['content'],true);
        }
        //表尾
        $list['footer'] = '';
        if(!empty($data['footer'])){
            $footer = json_decode($data['footer'],true);
            $list['footer'] = json_decode($footer['content'],true);
        }
        return $list;
	}
	/**
	 * [获取回答]
	 * @param  [type] $userId [用户ID]
	 * @param  [type] $taskid [课题ID]
	 * @param  [type] $pid    [description]
	 * @return [type]         [description]
	 */
	public function getanser($userId,$taskid,$pid){
            $param['userId']=$userId;
            $param['taskid']=$taskid;
            $param['classprogressid']=$pid;
            $data=Db::name('anser')->where($param)->order("pid asc,classprogressid asc")->select()->toArray();
            if(empty($data)){
                $data=array();
            }
            return $data;
    }
}